package com.qq2008.game.mojie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qq2008.game.mojie.jdo.Chat;
import com.qq2008.game.mojie.mapper.ChatMapper;
import com.qq2008.game.mojie.service.ChatService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements ChatService {

    @Override
    public Integer insertChat(Chat chat) {
        this.save(chat);
        return 1;
    }

    @Override
    public List<Chat> selectChat(Model model, HttpSession session) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        List<Chat> chats = this.list(new LambdaQueryWrapper<Chat>().orderByDesc(Chat::getTalkTime));
        for (Chat chat1 : chats) {
            String format1 = null;
            try {
                format1 = df.format(df2.parse(chat1.getTalkTime()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            chat1.setTalkTime(format1);
        }
        return chats;
    }
}
